# -*- coding: utf-8 -*-

'''eof
name:申请企业工商存续状态校验
code:R02_EG_EntStatusCheck
tableName:
columnName:
groups:企业业务信息校验
dependencies:EG_DSJ
type:常用指标
datasourceType:在线指标
description:
eof'''


import sys
import pandas as pd
import json
from datetime import datetime


def FormatData(tableName):
    try:
        eg = EG_DSJ.get("data").get("R1103V3")[0].get(tableName)
        return eg if isinstance(eg, list) else [{}]
    except:
        return [{}]


def convertCompanyStatus(type):
    if u"正常" in type:
        return u"正常"

    if (u"存续" in type) or (u"在营" in type) or (u"在业" in type) or (u"开业" in type) or (u"登记成立" in type) or (u"在册" in type):
        return u"存续"

    return type

def getRule02():
    null_type_list = ['', ' ', None, 'null', 'Null', 'NULL', '/', '-', '--']
    basic_list = FormatData("basicList")
    if basic_list == [{}]:
        return u'缺失值'
    else:
        if len(basic_list) >= 1:
            for i in basic_list:
                enterprise_type = i.get("enterpriseStatus")
                try:
                    result_type = convertCompanyStatus(enterprise_type)
                except:
                    return u'缺失值'
                if result_type not in null_type_list:
                    return result_type
                else:
                    return u'缺失值'
        else:
            return u'缺失值'


if __name__ == "__main__":
    file_obj = open(r'../data/rating/R02_EG_EntStatusCheck/EG_DSJ.json', 'r')
    content = file_obj.read()
    EG_DSJ = json.loads(content, strict=False)
    aa = getRule02()
    print aa

result = getRule02()

